// 创建一个uuid
export function uuid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    const r = (Math.random() * 16) | 0
    const v = c === 'x' ? r : (r & 0x3) | 0x8
    return v.toString(16).toUpperCase()
  })
}

// 复制文本到剪贴板
export function copy2Clipboard(text: string) {
  return new Promise((resolve, reject) => {
    const input = document.createElement('input')
    input.className = 'clipboard-hidden'
    input.value = text
    document.body.appendChild(input)
    input.select()

    if (document.execCommand('copy')) {
      resolve(true)
    } else {
      reject(new Error('您的浏览器不支持复制'))
    }

    document.body.removeChild(input)
  })
}
